export function setToken(token) {
  localStorage.setItem("token", token);
}

export function getToken() {
  return localStorage.getItem("token") || "";
}

/**
 * 将 list 转换成 tree
 */
export function listToTree(list) {
  const map = {};
  const result = [];

  list.forEach((item) => {
    map[item.id] = { ...item };
  });

  list.forEach((item) => {
    if (item.parentId) {
      if (map[item.parentId].children) {
        map[item.parentId].children.push(map[item.id]);
      } else {
        map[item.parentId].children = [map[item.id]];
      }
    } else {
      result.push(map[item.id]);
    }
  });

  return result;
}
